<?php
/**
 * Database
 * Class for the handling of SQL Queries
 * 
 * @license GNU/GPL 3
 * This package is distributed under the
 * GNU General Public License v.3.
 * See the LICENSE.txt file for details.
 * 
 * @package   Phlame
 * @author    Phlegma <phlegma@pheelgood.net>
 * @copyright 2008 by Phlegma, Phlame
 * @link       http://ugamela-forum.pheelgood.net
 * @since     01.05.2008
 * @version   0.4
 * @access    public
 * 
 * Thanks to meikel for ideas! The some of the DB::sqlInsert/Updates methodes are his work too. I used names of some other methodes, most of the code is new.
 * 
 * Last modify: 25.09.2008 (Phlegma)
 */

class DB
{
    protected $_connections = array();
    protected $_primaryId = NULL;
    #Singleton
    protected static $_instance = NULL;
    /**
     * @return DB
     */
    public static function getInstance()
    {
        if (self::$_instance === NULL) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    protected function __construct()
    {}
    private final function __clone() {}
    
    /**
     * Save a connection in the container
     * if there is no primary connection yet, it will be set as primary
     *
     * @param string $identifier
     * @param DatabaseConnection $res
     * @return DatabaseConnection
     */
    public function __set($identifier, DatabaseConnection $res = NULL)
    {
        if ($this->_primaryId === NULL) {
            $this->_primaryId = $identifier;
        }
        return $this->_connections[$identifier] = $res;
    }
    
    /**
     * Get a saved connection from the container
     *
     * @param string $identifier
     * @return DatabaseConnection
     */
    public function ___get($identifier)
    {
        if (!isset($this->_connections[$identifier])) {
            throw new DBException("Couldn't find any connection with identifier '$identifier'");
        }
        return $this->_connections[$identifier];
    }
    
    /**
     * Change the primary connection by choosing another ID
     *
     * @param string $identifier
     * @return DatabaseConnection
     */
    public function setPrimaryId($identifier = 0)
    {
        if (!isset($this->_connections[$identifier])) {
            throw new DBException("Couldn't find any connection with identifier '$identifier'");
        }
        $this->_primaryId = $identifier;
        return $this->_connections[$identifier];
    }
    
    /**
     * Get the primary connection
     *
     * @return DatabaseConnection
     */
    public function getPrimary()
    {
        if ($this->_primaryId === NULL  || $this->_connections[$this->_primaryId] === NULL) {
            throw new DBException('Primary is not set!');
        }
        return $this->_connections[$this->_primaryId];
    }
}
/*
interface DatabaseConfig {}

class MySQLiConfig implements DatabaseConfig
{
    public $host = 'localhost';
    public $user = 'root';
    public $passwd = '';
    public $dbname = 'test';
    
    public $praefix = '';//'ugml_';
    
    public function __construct(MySQLiConnection $c)
    {} 
}





function checkPHPConfig(){
    if (phpversion() < 5.1)
    {
        die('error: PHP version too old!');
    }

    if (mysql_get_client_info() < 5.0 || mysql_get_server_info() < 5.0)
    {
        die('error: MySQL version too old!');
    }

    if (ini_get('register_globals'))
    {
        die('error: register_globals = On');
    }
    if (get_magic_quotes_gpc())
    {
        die('error: magic_quotes_gpc = On');
    }
    if (ini_get('session.auto_start'))
    {
        die('error: session.auto_start = On');
    }
    if (! ini_get('session.use_cookies'))
    {
        die('error: session.use_cookies = Off');
    }
    if (! ini_get('session.use_only_cookies'))
    {
        die('error: session.use_only_cookies = Off');
    }
    if (ini_get('session.cookie_lifetime') < ini_get('session.gc_maxlifetime'))
    {
        die('error: session.cookie_lifetime < session.gc_maxlifetime');
    }
    if (ini_get('session.cookie_domain') != $_SERVER['SERVER_NAME'])
    {
        die('error: session.cookie_domain <> SERVER_NAME');
    }
    if (ini_get('session.gc_maxlifetime')< 3600)
    {
        die('error: session.gc_maxlifetime < 3600 sec');
    }
    if (ini_get('session.cookie_lifetime')<>ini_get('session.gc_maxlifetime'))
    {
        die('error: session.gc_maxlifetime <> session.cookie_lifetime');
    }
    if (! ini_get('session.cookie_httponly'))
    {
        die('error: session.cookie_httponly = Off');
    }
    if (ini_get('session.use_trans_sid'))
    {
        die('error: session.use_trans_sid = On');
    }
}
*/